import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;

// equals
class Student1{
    public int age;
    public String name;
    public Student1(String name, int age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        // 如果地址相等，返回true
        if (this == o) return true;

        // 如果o为空，或者o不是student的子类，返回false
        if (o == null || getClass() != o.getClass()) return false;

        Student1 student1 = (Student1) o;
        // name 是 String 引用类型，注意用equals比较
        return this.age == student1.age && Objects.equals(this.name, student1.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }
}

// Comparable
class Student implements Comparable<Student>{
    public int age;
    public String name;
    public Student(String name, int age) {
        this.age = age;
        this.name = name;
    }
    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
    }
}

// Comparator
// 比较器
class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}


class testComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        // Integer 中默认的比较是 o1.compareTo(o2)
        return o2.compareTo(o1);
    }
}

public class Test2 {
    public static void main7(String[] args) {
        Student student1 = new Student("李四",18);
        Student student2 = new Student("张三",20);

        AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();

        System.out.println(ageComparator.compare(student1,student2)); // -2
        System.out.println(nameComparator.compare(student1,student2)); // 2094
    }

    public static void main5(String[] args) {
        Student student1 = new Student("张三",18);
        Student student2 = new Student("张三",20);
        System.out.println(student1.compareTo(student2)); // 0
    }

    public static void main4(String[] args) {
        Student1 student1 = new Student1("张三",1);
        Student1 student2 = new Student1("张三",1);
        // 比较的是地址
        System.out.println(student1 == student2);// false
        // 比较的是内容
        System.out.println(student1.equals(student2));// true
    }

    public static void main(String[] args) {
        int[] array = { 22,29,15,42,35,62,34,56,19,31};
        PriorityQueue<Integer> priorityQueue1 = new PriorityQueue<>();
        for (int i = 0; i < array.length; i++) {
            priorityQueue1.offer(array[i]);
        }
        System.out.println(priorityQueue1);

        // 默认是小根堆
        // 如何创建大根堆呢
        // 手动传入一个 反过来的 比较器
        PriorityQueue<Integer> priorityQueue2 = new PriorityQueue<>(new testComparator());
        for (int i = 0; i < array.length; i++) {
            priorityQueue2.offer(array[i]);
        }
        System.out.println(priorityQueue2);
    }

    public static void main2(String[] args) {
        PriorityQueue<Student> priorityQueue3 = new PriorityQueue<>(new NameComparator());
        priorityQueue3.offer(new Student("张三",19));
        priorityQueue3.offer(new Student("李四",13));

        // 手动传了一个比较器
        // 如果传了比较器  就优先使用手动传入的方法进行比较
        PriorityQueue<Student> priorityQueue2 = new PriorityQueue<>(new AgeComparator());
        priorityQueue2.offer(new Student("张三",19));
        priorityQueue2.offer(new Student("李四",13));

        // 没传比较器，默认用类的实现的接口Comparable 中的compareTo 方法
        // 如果没有实现这个接口，也没传比较器，会抛出ClassCastException异常
        PriorityQueue<Student> priorityQueue1 = new PriorityQueue<>();
        priorityQueue1.offer(new Student("张三",19));
        priorityQueue1.offer(new Student("李四",13));
    }

    public static void main1(String[] args) {
        PriorityQueue<Student1> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(new Student1("张三",13));
        priorityQueue.offer(new Student1("李四",12));
        // PriorityQueue中放置 不能比较大小的 第二个元素时，会抛出ClassCastException异常
        // priorityQueue.offer(null);
        // 插入null对象，会抛出NullPointerException
    }
}
